package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Friend;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FriendApiImpl implements FriendApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public  Boolean  contacts(Long to, Long userId) {
        try {
            Query query = new Query();
            query.addCriteria(Criteria.where("userId").is(to).and("friendId").is(userId));
            boolean exists = mongoTemplate.exists(query, Friend.class);
            if (!exists) {
                Friend friend = new Friend();
                friend.setFriendId(userId);
                friend.setCreated(System.currentTimeMillis());
                friend.setUserId(to);
                mongoTemplate.save(friend);
            }

            Query query1 = new Query();
            query1.addCriteria(Criteria.where("userId").is(userId).and("friendId").is(to));
            boolean exists1 = mongoTemplate.exists(query1, Friend.class);
            if (!exists1) {
                Friend friend = new Friend();
                friend.setFriendId(to);
                friend.setCreated(System.currentTimeMillis());
                friend.setUserId(userId);
                mongoTemplate.save(friend);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public List<Friend> getByUserId(Long userId, Integer page, Integer pagesize) {
        Query query = new Query();
        if (page != null && pagesize != null) {
            query.addCriteria(Criteria.where("userId").is(userId))
                    .with(PageRequest.of(page - 1, pagesize))
                    .with(Sort.by(Sort.Direction.DESC, "created"));
        }else {
            query.addCriteria(Criteria.where("userId").is(userId))
                    .with(Sort.by(Sort.Direction.DESC, "created"));
        }
        return mongoTemplate.find(query, Friend.class);
    }

    @Override
    public void unContactsTo(Long userId, Long likeUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("friendId").is(likeUserId));
        boolean exists = mongoTemplate.exists(query, Friend.class);
        if (exists) {
            mongoTemplate.remove(query, Friend.class);
        }

        Query query1 = new Query();
        query1.addCriteria(Criteria.where("userId").is(likeUserId).and("friendId").is(userId));
        boolean exists1 = mongoTemplate.exists(query1, Friend.class);
        if (exists1) {
            mongoTemplate.remove(query1, Friend.class);
        }

    }
}
